<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="活动名称" prop="activityName">
        <el-input v-model="queryParams.activityName" placeholder="请输入活动名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="活动部门" prop="activityDeptId">
        <!-- <el-input v-model="queryParams.activityDeptId" placeholder="请输入活动部门" clearable @keyup.enter="handleQuery" /> -->
        <el-tree-select style="width:100%" v-model="queryParams.activityDeptId" :data="deptOptions" @node-click="handleNodeClick($event,'queryParams')"
          :props="{ value: 'id', label: 'label', children: 'children' }" value-key="id" placeholder="请选择活动部门" check-strictly clearable />
      </el-form-item>
      <el-form-item label="类型" prop="activityType">
        <el-select v-model="queryParams.activityType" placeholder="请选择" clearable>
          <el-option v-for="dict in activity_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['charge:activity:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['charge:activity:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['charge:activity:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['charge:activity:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="activityList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" align="center" width="65" type="index" />
      <el-table-column label="月份" align="center" prop="activityName" />
      <el-table-column label="收费站" align="center" prop="activityName" />
      <el-table-column label="活动部门" align="center" prop="deptName" />
      <el-table-column label="活动时间" align="center" prop="activityTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.activityTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="活动类型" align="center" prop="activityType">
        <template #default="scope">
              <el-tag type="primary">
              <dict-tag :options="activity_type" :value="scope.row.activityType" /></el-tag>
            </template>
      </el-table-column>
      <el-table-column label="活动方式" align="center" prop="activityMode">
        <template #default="scope">
          <dict-tag :options="activity_mode" :value="scope.row.activityMode" />
        </template>
      </el-table-column>
      <el-table-column label="活动地点" align="center" prop="activityPlace" />
      <el-table-column label="现场照片" align="center" prop="imgList">
        <template #default="scope">
          <image-preview :src="scope.row.imgList" :width="50" :height="50" />
          <!-- <el-image style="width: 100px; height: 100px" :src="url" :zoom-rate="1.2" :max-scale="7" :min-scale="0.2" :preview-src-list="srcList"
            :initial-index="4" fit="cover" /> -->
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <span>{{scope.row.status==0?'未完成':'完成'}}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['charge:activity:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['charge:activity:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改活动对话框 -->
    <el-dialog :title="title" v-model="open" width="1000px" append-to-body>
      <el-form ref="activityRef" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="活动名称" prop="activityName">
              <el-input v-model="form.activityName" placeholder="请输入活动名称" style="width:100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="活动部门" prop="activityDeptId">
              <el-tree-select style="width:100%" v-model="form.activityDeptId" :data="deptOptions" @node-click="handleNodeClick($event,'form')"
                :props="{ value: 'id', label: 'label', children: 'children' }" value-key="id" placeholder="请选择活动部门" check-strictly clearable />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="活动时间" prop="activityTime">
              <el-date-picker style="width:100%" clearable v-model="form.activityTime" type="date" value-format="YYYY-MM-DD" placeholder="请选择活动时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="活动类型" prop="activityType">
              <el-select style="width:100%" v-model="form.activityType" placeholder="请选择活动类型">
                <el-option v-for="dict in activity_type" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="活动方式" prop="activityMode">
              <el-select style="width:100%" v-model="form.activityMode" placeholder="请选择活动方式">
                <el-option v-for="dict in activity_mode" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item style="width:100%" label="活动人数" prop="activityPerson">
              <el-input v-model="form.activityPerson" placeholder="请输入活动人数" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item style="width:100%" label="活动地点" prop="activityPlace">
              <el-input v-model="form.activityPlace" placeholder="请输入活动地点" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="相关文件" prop="fileList" v-if="isEdit">
          <div class="img-box">
            <CusUpload :modelValue="form.fileList1" @update:modelValue="updateValue($event,0)" :limit="1" :isShowTip="false" name="演练通知" />
            <div class="img-tips">演练通知</div>
          </div>
          <div class="img-box">
            <CusUpload :modelValue="form.fileList2" @update:modelValue="updateValue($event,1)" :isShowTip="false" name="演练方案" />
            <div class="img-tips">演练方案</div>
          </div>
          <div class="img-box">
            <CusUpload :modelValue="form.fileList3" @update:modelValue="updateValue($event,2)" :isShowTip="false" name="情况登记表" />
            <div class="img-tips">情况登记表</div>
          </div>
          <div class="img-box">
            <CusUpload :modelValue="form.fileList4" @update:modelValue="updateValue($event,3)" :isShowTip="false" name="效果评估表" />
            <div class="img-tips">效果评估表</div>
          </div>
          <div class="img-box">
            <CusUpload :modelValue="form.fileList5" @update:modelValue="updateValue($event,4)" :isShowTip="false" name="总结" />
            <div class="img-tips">总结</div>
          </div>
          <div class="img-box">
            <CusUpload :modelValue="form.fileList6" @update:modelValue="updateValue($event,5)" :isShowTip="false" name="签到表" />
            <div class="img-tips">签到表</div>
          </div>

        </el-form-item>
        <el-form-item label="现场照片" prop="imgList" v-if="isEdit">
          <ImageUpload :modelValue="form.imgList" v-model="form.imgList" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input style="width:100%" v-model="form.remark" type="textarea" placeholder="请输入内容" :rows="5"  />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Activity">
import { deptTreeSelect } from "@/api/system/user";
import { listActivity, getActivity, delActivity, addActivity, updateActivity } from "@/api/charge/activity";
import CusUpload from "@/components/Custom/CusUpload"
const { proxy } = getCurrentInstance();
const { activity_type, activity_mode } = proxy.useDict('activity_type', 'activity_mode');

const activityList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const deptOptions = ref([])
const isEdit=ref(true)

const data = reactive({
  form: {
    fileList: [],
    fileList1: [],
    fileList2: [],
    fileList3: [],
    fileList4: [],
    fileList5: [],
    fileList6: [],
  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    // activityTime:'2024',
    activityName: null,
    activityDeptId: null,
    status: null
  },
  rules: {
    activityName: [
      { required: true, message: '活动名称不能为空', trigger: 'blur' },
    ],
    activityDeptId: [
      { required: true, message: '活动部门不能为空', trigger: 'blur' },
    ],
    activityTime: [
      { required: true, message: '活动时间不能为空', trigger: 'blur' },
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);
/** 查询部门下拉树结构 */
function getDeptTree() {
  deptTreeSelect().then(response => {
    deptOptions.value = response.data;
  });
};
/** 查询活动列表 */
function getList() {
  loading.value = true;
  listActivity(queryParams.value).then(response => {
    activityList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    activityName: null,
    activityDeptId: null,
    activityTime: null,
    activityType: null,
    activityMode: null,
    activityPlace: null,
    imgList: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null,
    status: null,
    fileList: [],
    fileList1: [],
    fileList2: [],
    fileList3: [],
    fileList4: [],
    fileList5: [],
    fileList6: [],
  };
  proxy.resetForm("activityRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  isEdit.value=false;
  open.value = true;
  title.value = "添加活动";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  
  reset();
  isEdit.value=true;
  const _id = row.id || ids.value
  getActivity(_id).then(response => {
    form.value = response.data;
    if (response.data && response.data.fileList && response.data.fileList.length) {
      response.data.fileList.forEach(item => {
        switch (item.name) {
          case '演练通知':
            form.value.fileList1 = [item]
            break;
          case '演练方案':
            form.value.fileList2 = [item]
            break;
          case '情况登记表':
            form.value.fileList3 = [item]
            break;
          case '效果评估表':
            form.value.fileList4 = [item]
            break;
          case '总结':
            form.value.fileList5 = [item]
            break;
          case '签到表':
            form.value.fileList6 = [item]
            break;
        }

      })
    }



    open.value = true;
    title.value = "修改活动";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["activityRef"].validate(valid => {
    if (valid) {
      delete form.value.fileList1;
      delete form.value.fileList2;
      delete form.value.fileList3;
      delete form.value.fileList4;
      delete form.value.fileList5;
      delete form.value.fileList6;


      if (form.value.id != null) {
        updateActivity(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addActivity(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
function handleNodeClick(data, name) {
  if (name == 'queryParams') {
    queryParams.value.activityDeptId = data.id;
  } else {
    form.value.activityDeptId = data.id;
  }


}
/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除活动编号为"' + _ids + '"的数据项？').then(function () {
    return delActivity(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('charge/activity/export', {
    ...queryParams.value
  }, `activity_${new Date().getTime()}.xlsx`)
}

const updateValue = (data, index) => {
  let name = 'fileList' + (index + 1);
  if (data[0].url) {
    form.value[name] = data;
    form.value.fileList.push(data[0])
  } else {
    form.value[name] = [];
    let delIndex = -1;
    form.value.fileList.forEach((item, i) => {
      if (item.name == data[0].name) {
        delIndex = i
      }
    })
    form.value.fileList.splice(delIndex, 1)
  }



  // form.value.fileList[index] = data[0]
  // form.value.fileList[index].activityId = data[0].uid
  // form.value.fileList[index].id = data[0].uid
}

// const updateValue = (data, index) => {
//   let name='fileList'+index

//   form.value[name] = data[0]
//   // form.value.fileList[index].activityId = data[0].uid
//   // form.value.fileList[index].id = data[0].uid
// }

getDeptTree()
getList();
</script>

<style scoped>
.img-box {
  margin-right: 15px;
}
.img-box .img-tips {
  font-size: 12px;
  line-height: 20px;
  text-align: center;
  margin-top: 10px;
}
</style>
